package myShoppingCart

import (
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/controllers/login"
	USER "OnlineRetailers/models/User"
	error2 "OnlineRetailers/models/errorDeal"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type UpdateMyShoppingCart struct {
	beego.Controller
}

func (c *UpdateMyShoppingCart) UpdateMyShoppingCart() {

	defer func() {
		c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()

	var addr = "/myShoppingCart/UpdateMyShoppingCart"
	var err error

	shoppingCart := &USER.ShoppingCart{}
	log := &error2.Log{}
	mes := &error2.Mes{}

	o := orm.NewOrm()

	err = json.Unmarshal([]byte(c.GetString("shoppingCart")), shoppingCart)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	//fmt.Println(shoppingCart)
	err, _ = login.CheckOpenid(c.Ctx.Request.Header["Openid"])
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	num, err := o.Update(shoppingCart)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	if num == 0 {
		c.Data["json"] = errorDeal.ErrMess(log, "400", fmt.Errorf("num==0"), addr, "num==0")
		return
	}

	mes.Mes = "success"
	mes.Code = "200"
	c.Data["json"] = mes
}
